<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Giohang extends MY_Controller {
	/*
		*
		* Ham LoadPage() trong MY_Controller
		*
	*/
	public function __construct()
    {
        parent::__construct();	
		$this->load->model('gioHang_model');
		$this->loadtrangchu(""); 
		$this->load->helper('url');
	} 
	//$this->load->model('Sanpham_model'); 
	public function GioHang()
	{    
		$this->data['title']=$this->lang->line('TD_GioHang');//"Giỏ hàng";
		$this->data['GH_ThongBao']="";
		$this->LoadPage('trangchu/GioHang', $this->data); 
	} 
	public function AddGioHang()
	{  		
		$this->data['SanPhamMua'] = $this->gioHang_model->LaySanPham($this->uri->segment(4)); 
		if($this->data['SanPhamMua']['SoLuong']>0){
			$this->data['GioHang'] = array(
				   array(
						   'id'      => $this->data['SanPhamMua']['MaSP'],
						   'qty'     => 1,
						   'price'   => $this->data['SanPhamMua']['DonGia'],
						   'name'    => $this->data['SanPhamMua']['TenSanPham'],
						   'Hinh'    => $this->data['SanPhamMua']['Hinh']
				   )
			 ); 
			$this->cart->insert($this->data['GioHang']); 
			$this->data['GH_ThongBao']="Bạn đã thêm vào giỏ hàng thành công.";
		}
		else{$this->data['GH_ThongBao']="Đã hết hàng.";}
		$this->data['title']=$this->lang->line('TD_GioHang');//"Giỏ hàng"; 
		$this->LoadPage('trangchu/GioHang', $this->data); 
	} 
	public function UpGiohang()
	{ 
		$this->load->helper('form'); 
		$data = $this->input->post();
		$this->cart->contents();
		$i=0; $j=0;
		$Bien = array() ;
		foreach($this->cart->contents() as $item)
		{  
			 $Bien[$i]=$item["id"]; 
			  $i++;
		}  
		foreach($data as $item)
		{ 
			$this->data['SanPhamMua'] = $this->gioHang_model->LaySanPham($Bien[$j]); 
			if($this->data['SanPhamMua']['SoLuong']>=$item['qty']){
				$info = array(
						'rowid' => $item['rowid'],
						'qty' => $item['qty']
				);  
			$this->cart->update($info);
			}
			$j++;
		}			
		$this->GioHang();
	 }
	public function DelGiohang($rowid) {
        
        $this->cart->update(array(
            'rowid' => $rowid,
            'qty' => 0
        )); 
        $this->GioHang(); 
    }
	public function DelAllGiohang() {
        
        $this->cart->destroy();
        $this->GioHang(); 
    }
	
	  
}